



//
//  user.swift
//  mywei
//
//  Created by myd on 15/12/17.
//  Copyright © 2015年 myd1. All rights reserved.
//

import UIKit

class user1: NSObject{
//    aCoder.encodeObject(access_token, forKey: "access_token")
//    aCoder.encodeDouble(expires_in, forKey: "expires_in")
//    aCoder.encodeObject(uid, forKey: "uid")
//    aCoder.encodeObject(expires_date, forKey: "expires_date")
//    aCoder.encodeObject(name, forKey: "name")
//    aCoder.encodeObject(avatar_large, forKey: "avatar_large"
    
    var expires_date : NSDate?
    var expires_in: NSTimeInterval = 0 {
    didSet {
    expires_date = NSDate(timeIntervalSinceNow: expires_in)
    }
    }
    
    
    var screen_name : String?
    var uid : String?
    var access_token : String?
    var avatar_large : String?

    //    @"https://api.weibo.com/2/users/show.json"
    private static var us : user1? {
        get{
   let  uus = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? user1
        return uus
        }}
  static  let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! + "saveaccess_token.plist"

    
    
    
    
    
    
    init(dict: [String: AnyObject ]) {
    super.init()
        setValuesForKeysWithDictionary(dict)
    }
       override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    func saveaccess_token()
    {
    NSKeyedArchiver.archiveRootObject(self, toFile: user1.path)
    }
   
  class  func loadtoken () -> user1?
    {
//        if us==nil{
//            us = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? user1}
        if(us != nil){
    
        if us != nil && us?.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending
        {
         return us
        }else
        {
       print("账号过期")
            return nil
            }
        }
            
            else{
           print("还没有登陆")
                return nil
            }
        }
    
    func loaduserinfo(finsh1 : (error1 : NSError?) ->() )
    {
        OauthNetwork.sharedinstance.loaduserinfo { (result, Error) -> () in
            if result==nil || Error != nil
            {
           print("错误")
                finsh1(error1 : Error)
                return
            }
            self.screen_name = result!["screen_name"] as? String
            self.avatar_large = result!["avatar_large"] as? String
            self.saveaccess_token()
            finsh1(error1: nil)
        }
        
    }
    class func loadboo() -> Bool {
        
        return self.loadtoken() != nil
    }
    
    
    required init?(coder aDecoder: NSCoder)
    {
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String

//        var remind_in : String?
    }
    
   
    
}


extension user1 : NSCoding
{
    func encodeWithCoder(aCoder: NSCoder)
    {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
      
    }


}